///登录页面
<template>
  <div class="loginbackground">
    <el-row>
      <el-col :span="12">
      </el-col>
      <el-col :span="10" class="el-col-login">
        <div class="el-form-div">
          <div class="image-flag">
          </div>
          <el-form>
            <el-form-item label="账号：">
              <el-input type="text" v-model="loginname" />
            </el-form-item>
            <el-form-item label="密码：">
              <el-input type="password" v-model="password"  @keydown.enter="Submit" />
            </el-form-item>
          </el-form>
          <div class="login-button">
            <el-button type="primary" style="width: 360px;" size="large" @click="Submit">登&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;录</el-button>
          </div>
        </div>
      </el-col>
      <el-col :span="2">
      </el-col>
    </el-row>

  </div>
</template>

<script setup>
import { ref, inject } from "vue";
import { useRouter } from "vue-router";
import { useLoginUserStore } from "@/store";

const router = useRouter();
// 使用 Pinia store
const loginUserStore = useLoginUserStore(); // 创建 userStore 实例

//获取API配置
const API = inject("API");
const ApiConfig = inject("ApiConfig");

const loginname = ref("");
const password = ref("");

const Submit = async () => {
  try {
    // 调用 API 发送登录请求
    const response = await API.request(
      ApiConfig.Methods.GET,
      ApiConfig.PATH.Account.Login,
      { username: loginname.value, password: password.value }
    );
    if (response.status == "200" && response.data.code == "200") {
      //登录用户信息
      const currentUser = response.data.data; // 获取当前登录用户信息
      loginUserStore.loginUser(currentUser, response.data.token);
      // 跳转到首页或者其他默认页面
      router.push({ name: "MainForm" });
    } else {
    }
  } catch (error) {
    //重定向处理
    console.log(error);
  }
};
</script>